<?php
namespace app\controller;

use think\Request;
use think\facade\Db;

class Home extends Common
{

	private function formatDate($d){
		if( $d <10 ) {
			return '0'.$d;
		}
		return $d;
	}

	/**
	 * 查询数据
	 *
	 * @return
	 */
	public function getCount(Request $request) {

		$loginIP = '';
		$loginTime = '';
		$logininfo = Db::query("SELECT * FROM `cfg_logs` WHERE `companyId` = '{$request->sessInfo['companyId']}' AND `type` = 1 AND `user` = '{$request->sessInfo['username']}' AND `url` = '/login/index' ORDER BY `id` DESC LIMIT 1, 1");
		if(isset($logininfo[0]['time'])){
			$loginTime = date('Y-m-d H:i:s', $logininfo[0]['time']);
		}
		if(isset($logininfo[0]['ip'])){
			$loginIP = $logininfo[0]['ip'];
		}

		$loginIPName = '';
		if($loginIP) {
			$address = new \Net\IpLocation( app()->getRootPath() . 'data' . DIRECTORY_SEPARATOR . 'qqwry.dat');
			$loginIPName = ' => ' . $address->getAddrName($loginIP);
		}

		$cacheName = function($request, $name) {
			return $request->sessInfo['companyId'] . '_cache_' . $name;
		};

		//echo Db::getLastSql() . "\n";
	  $chartData = [];
		for ($i = 1; $i <= date('t'); $i++) {
			if( $i == date('j')) {
				break;
			}
			$Date = $i . '号';
			$dateName = date('Y-m-').$i;
		  $dateTime = cloud_datetime_format(date('Y-m-d 00:00:00', strtotime($dateName)), date('Y-m-d 00:00:00', strtotime( $dateName . " +1 day")), true);
 			$accessNumber = Db::table('cfg_access')->where('companyId', $request->sessInfo['companyId'])->where('accessTime', 'between', [ date('Y-m-d 00:00:00', $dateTime[0]), date('Y-m-d 00:00:00', $dateTime[1])])->cache($cacheName($request, 'home_daypv_'.$dateName), 86400)->count();
			$copyNumber = Db::table('cfg_search')->where('companyId', $request->sessInfo['companyId'])->where('datatime', 'between', $dateTime)->cache($cacheName($request, 'home_daycopy_'.$dateName),86400)->sum('copynumber');
			$copyCost = Db::table('cfg_search')->where('companyId', $request->sessInfo['companyId'])->where('datatime', 'between', $dateTime)->cache($cacheName($request, 'home_daycost_'.$dateName), 86400)->sum('copycost');
			array_push($chartData, ['Date'=> $Date, 'Name' => '访问数', 'Value' => $accessNumber ]);
			array_push($chartData, ['Date'=> $Date, 'Name' => '复制数', 'Value' => $copyNumber ]);
			array_push($chartData, ['Date'=> $Date, 'Name' => '复制成本', 'Value' => $copyCost ]);
		}

    $dayPv = Db::table('cfg_access')->where('companyId', $request->sessInfo['companyId'])->whereTime('accessTime', 'today')->count();
		$dayCopyNum = Db::table('cfg_search')->where('companyId', $request->sessInfo['companyId'])->whereTime('datatime', 'today')->sum('copynumber');
		$dayCopyCost = Db::table('cfg_search')->where('companyId', $request->sessInfo['companyId'])->whereTime('datatime', 'today')->sum('copycost');

		$weekTime = cloud_datetime_format(date('Y-m-d 00:00:00', strtotime("-7 day")), date('Y-m-d 00:00:00'), true);
		$weekPv = Db::table('cfg_access')->where('companyId', $request->sessInfo['companyId'])->where('accessTime', 'between', [ date('Y-m-d 00:00:00', $weekTime[0]), date('Y-m-d 00:00:00', $weekTime[1])])->cache($cacheName($request, 'home_week_1'),86400)->count();
		$weekCopyNum = Db::table('cfg_search')->where('companyId', $request->sessInfo['companyId'])->where('datatime', 'between', $weekTime)->cache($cacheName($request, 'home_week_2'),86400)->sum('copynumber');
		$weekCopyCost = Db::table('cfg_search')->where('companyId', $request->sessInfo['companyId'])->where('datatime', 'between', $weekTime)->cache($cacheName($request, 'home_week_3'),86400)->sum('copycost');

    $monthTime = cloud_datetime_format(date('Y-m-01 00:00:00'), date('Y-m-d 00:00:00'), true);
    $monthPv = Db::table('cfg_access')->where('companyId', $request->sessInfo['companyId'])->where('accessTime', 'between', [ date('Y-m-d 00:00:00', $monthTime[0]), date('Y-m-d 00:00:00', $monthTime[1])])->cache($cacheName($request, 'home_month_1'), 86400)->count();
		$monthCopyNum = Db::table('cfg_search')->where('companyId', $request->sessInfo['companyId'])->where('datatime', 'between', $monthTime)->cache($cacheName($request, 'home_month_2'), 86400)->sum('copynumber');
		$monthCopyCost = Db::table('cfg_search')->where('companyId', $request->sessInfo['companyId'])->where('datatime', 'between', $monthTime)->cache($cacheName($request, 'home_month_3'), 86400)->sum('copycost');

		return response(json_encode(['code'=> 0, 'address' => $request->ip(), 'loginIP'=> $loginIP, 'loginIPName'=> $loginIPName,'loginTime'=> $loginTime,
			'dayPv'=> $dayPv,
			'weekPv'=> $weekPv,
			'monthPv'=> $monthPv,
			'chartData' => $chartData,
			'dayCopyNum'=> $dayCopyNum,
			'dayCopyCost'=> $dayCopyCost,
			'weekCopyNum'=> $weekCopyNum,
			'weekCopyCost'=> $weekCopyCost,
			'monthCopyNum'=> $monthCopyNum,
			'monthCopyCost'=> $monthCopyCost,
			]))->header(['Content-Type' => 'application/json; charset=utf-8']);
	}

}